package GameMain.View.RoomList
{
	import Engine.Module.ModuleBase;
	
	import GameMain.Game;
	import GameMain.Macro.Macro;
	import GameMain.Macro.MacroEvent;
	import GameMain.View.Room.Room;
	
	import Interface.Module.IModuleBase;
	import Interface.UIControl.IButton;
	import Interface.UIControl.ITextBase;
	
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	
	import library.SocketData.SocketData;
	
	public class RoomList extends ModuleBase
	{
		
		private var _roomItemList:Array = new Array();
		private var _txt_playName:ITextBase;
		private var _txt_playLevel:ITextBase;
		private var _pLevleArea:PLevelArea;
		
		public function RoomList(p_content:Sprite,info:LoaderInfo)
		{
			super(p_content);
			Game.ControlMgr.getButton(this._content["FAST_JOIN"]);
			Game.ControlMgr.getButton(this._content["CREATE_ROOM"]).setOnClickFun(onCreateRoom);
			Game.ControlMgr.getButton(this._content["SEARCH_ROOM"]);
			
			Game.ControlMgr.getButton(this._content["PRE_PAGE"]).setOnClickFun(onPrePage);
			Game.ControlMgr.getButton(this._content["NEXT_PAGE"]).setOnClickFun(onNextPage);
			
			PLevelArea.setItemClass(info.applicationDomain.getDefinition("Fla.ViewMain_PlayerItem") as Class);
			
			this._txt_playLevel = Game.ControlMgr.getText(this._content["P_LEVEL_TXT"]);
			this._txt_playName =  Game.ControlMgr.getText(this._content["P_NAME_TXT"]);
			
			this.initUI();
			
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_JOIN_ROOM,onJoinRoom);
		}
		
		private function initUI():void
		{
			var tx:int=18;var ty:int =102;
			for(var i:int=1;i<9;i++)
			{
				var r:RoomListItem = new RoomListItem(this._content["ROOM_ITEM_"+i]);
				_roomItemList.push(r);
				this.addChild(r.content);
				r.setDisable();
			}
			this._pLevleArea = new PLevelArea(this._content["PM_LIST"]);
		}
		
		private function setRoomItemVisible():void
		{
			for(var i:int=0;i<this._roomItemList.length;i++)
			{
				var r:RoomListItem = this._roomItemList[i];
				r.setDisable();
			}
		}
		
		private function onCreateRoom():void
		{
			Game.EngineMgr.sendMessageData(MacroEvent.MSG_TO_SERVER_CREATE_ROOM,null);
		}
		
		private function onJoinRoom(data:SocketData):void
		{
			var imd:IModuleBase = Game.ModuleMgr.getModule(Macro.MODULE_ROOM);
			(imd as Room).onJoinRoom(data);
			Game.ModuleMgr.showRoom();
		}
		
		private function onGetRoomList(data:SocketData):void
		{
			this.setRoomItemVisible();
			var roomNum:int = data.readInt16();
			var r:RoomListItem;
			for(var i:int = 0; i< roomNum;i++)
			{
				var roomID:int = data.readInt16();
				var currNum:int = data.readInt8();
				var totalNum:int = data.readInt8();
				var roomName:String = data.readString();
				r = this._roomItemList[i];
				r.setRoomItemInfo(roomID,currNum,totalNum,roomName);
				r.setAble();
			}
		}
		
		private function onPrePage():void
		{
			var data:SocketData = new SocketData();
			data.writeInt8(2);
			this.sendMsg(MacroEvent.MSG_TO_SERVER_GET_ROOM_LIST,data);
		}
		
		private function onNextPage():void
		{
			var data:SocketData = new SocketData();
			data.writeInt8(1);
			this.sendMsg(MacroEvent.MSG_TO_SERVER_GET_ROOM_LIST,data);
		}
		
		override protected function onShowBack():void
		{
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_ROOM_LIST,onGetRoomList);
			this._txt_playName.text = Game.ownPlayer.playerName;
			this._txt_playLevel.text = Game.ownPlayer.playerLevel.toString() + "级";
		}
		
		override protected function onDisposeBack():void
		{
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_ROOM_LIST);
			this.setRoomItemVisible();
		}
	}
}